<template>
  <div class="add">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="studentName"> 
        <el-input v-model="ruleForm.studentName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-input v-model="ruleForm.sex"></el-input>
      </el-form-item>
      <el-form-item label="学院" prop="institute">
        <el-input v-model="ruleForm.institute"></el-input>
      </el-form-item>
      <el-form-item label="所属专业" prop="major">
        <el-input v-model="ruleForm.major"></el-input>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-input v-model="ruleForm.grade"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="clazz">
        <el-input v-model="ruleForm.clazz"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="tel">
        <el-input v-model="ruleForm.tel"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="cardId">
        <el-input v-model="ruleForm.cardId"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="cancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validateName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生名字不能为空'));
        }else{
          callback();
        }
      };
      var validateSex = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生性别不能为空'));
        }else{
          callback();
        }
      };
       var validateMajor = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生专业不能为空'));
        }else{
          callback();
        }
      };
      var validateClazz = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生所属班级不能为空'));
        }else{
          callback();
        }
      };
       var validateInstitute = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生所属学院不能为空'));
        }else{
          callback();
        }
      };
       var validateGrade = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生所属年级不能为空'));
        }else{
          callback();
        }
      };

      var validateTel = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生电话不能为空'));
        }else{
          callback();
        }
      };
       var validateCardId = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('学生身份证号不能为空'));
        }else{
          callback();
        }
      };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        studentName: "",
        sex: "",
        institute: "",
        major: "",
        grade: "",
        clazz: "",
        tel: "",
        cardId: "",
        email: "",
        pass: "",
        checkPass: "",
        role: 2
      },
      rules: {
        studentName:[
          {validator:validateName,trigger: 'blur'}
        ],
         grade:[
          {validator:validateGrade,trigger: 'blur'}
        ],
         major:[
          {validator:validateMajor,trigger: 'blur'}
        ],
        clazz:[
          {validator:validateClazz,trigger: 'blur'}
        ],
        institute:[
          {validator:validateInstitute,trigger: 'blur'}
        ],
        tel:[
          {validator:validateTel,trigger: 'blur'}
        ],
        sex:[
          {validator:validateSex,trigger: 'blur'}
        ],
        cardId:[{validator:validateCardId,trigger:'blur'}],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios({
        url: '/api/student',
        method: 'post',
        data: {
          ...this.ruleForm
        }
      }).then(res => {
        if(res.data.code == 200) {
          this.$message({
            message: '数据添加成功',
            type: 'success'
          })
          this.$router.push({path: '/'})
        }
      })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    cancel() {
      //取消按钮
     this.$router.push({path: '/'});
    },
  },
};
</script>

<style lang="scss"  scoped>
.add {
  margin-top: 80px;
  padding: 0px 40px;
  width: 400px;
}
</style>